// 切换
// 用户名登录
$("#userlogin").click(function() {
	$(this).css({
		borderBottom: "2px solid brown",
		color: "brown"
	});
	$("#phonelogin").css({
		borderBottom: "none",
		color: "#999"
	});
	$("#form1").css({
		display: "block"
	});
	$("#form2").css({
		display: "none"
	});

});
// 是否输入用户名和密码
$("#userphone").blur(function() {
	if ($(this).val() == "") {
		$("#form-error1").css({
			display: "block"
		});
	} else {
		$("#form-error1").css({
			display: "none"
		});
	}
});
$("#userpwd").blur(function() {
	if ($(this).val() == "") {
		$("#form-error2").css({
			display: "block"
		});
	} else {
		$("#form-error2").css({
			display: "none"
		});
	}
});
if ($(".loginto #a2").html("注册" + `<img src="img/denlu.png">`)) {
	$(".loginto #a2").click(function() {
		location.href = "Register.html";
	});
}

$("#user-btn").click(function() {
	$.ajax({
		url: "php/userlogin.php",
		data: {
			userphone: $("#userphone").val(),
			userpwd: $("#userpwd").val()
		},
		type: "get",
		success: function(res) {
			if (res == "1") {
				alert("登录成功");
				// location.href = "Home.html";
				$(".loginto #a1").html($("#userphone").val());
				$(".loginto #a2").html("退出" + `<img src="img/denlu.png">`);
				if ($(".loginto #a2").html("退出" + `<img src="img/denlu.png">`)) {
					$(".loginto #a2").click(function() {
						location.href = "Home.html";
						// $(".loginto #a1").html("登录");
						// $(".loginto #a2").html("注册"+`<img src="img/denlu.png">`);
					});
				}
				let ss = sessionStorage;
				ss.setItem("username", $("#userphone").val());
				ss.setItem("userpwd", $("#userpwd").val());
			} else {
				alert("没有此用户，请注册");
				// location.href = "Register.html";
			}
		}
	});
});



// 手机号快捷登录
$("#phonelogin").click(function() {
	$(this).css({
		borderBottom: "2px solid brown",
		color: "brown"
	});
	$("#userlogin").css({
		borderBottom: "none",
		color: "#999"
	});
	$("#form1").css({
		display: "none"
	});
	$("#form2").css({
		display: "block"
	});
});
$("#phone-btn").click(function() {
	$.ajax({
		url: "php/phonelogin.php",
		data: {
			userphone: $("#phone").val(),
			userpwd: $("#sms").val()
		},
		type: "get",
		success: function(res) {
			if (res == "1" && flagregcode == true) {
				alert("登录成功");
				// location.href = "Home.html";
				$(".loginto #a1").html($("#phone").val());
				$(".loginto #a2").html("退出" + `<img src="img/denlu.png">`);
				if ($(".loginto #a2").html("退出" + `<img src="img/denlu.png">`)) {
					$(".loginto #a2").click(function() {
						location.href = "Home.html";
					});
				}

				let ss = sessionStorage;
				ss.setItem("userphonename", $("#phone").val());
				ss.setItem("usersms", $("#sms").val());
			} else {
				alert("没有此用户，请注册");
			}
		}
	});
});
$(".button2").click(function(){
	location.href="Register.html";
})





// 图标切换
let img1 = $(".img1");
let img2 = $(".img2");
for (let i = 0; i < img1.length; i++) {
	$(img1[i]).mouseover(function() {
		$(this).css({
			display: "none"
		});
		$(img2[i]).css({
			display: "inline-block"
		});
	});
	$(img1[i]).mouseout(function() {
		$(this).css({
			display: "inline-block"
		});
		$(img2[i]).css({
			display: "none"
		});
	});
}

// 判断手机号是否输入正确
let regphone = /^1\d{10}$/;
let flagregphone = false;
$("#phone").blur(function() {
	if (regphone.test($(this).val())) {
		$("#form-c-error1").css({
			display: "none"
		});
		flagregphone = true;
	} else {
		$("#form-c-error1").css({
			display: "block"
		});
		flagregphone = false;
	}
});


// 短信验证码是否输入
$("#sms").blur(function() {
	if ($(this).val() == "") {
		$("#form-c-error3").css({
			display: "block"
		});
	} else {
		$("#form-c-error3").css({
			display: "none"
		});
	}
});




// 图形验证码
// 随机产生验证码并判断验证码是否输入正确


function createCode() {
	var regcode = "";
	let codeLength = 4; //验证码的长度   
	let random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
		'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); //随机数  
	for (let i = 0; i < codeLength; i++) { //循环操作  
		let index = Math.floor(Math.random() * 36); //取得随机数的索引（0~35）  
		regcode += random[index]; //根据索引取得随机数加到code上  
	}
	$("#captcha").html(regcode); //把code值赋给验证码 
}
createCode();

$(".form-c-img").click(function() {
	createCode();
});

let flagregcode = false;
$("#code").blur(function() {
	if ($(this).val() != $("#captcha").html()) {
		$("#form-c-error2").css({
			display: "block"
		});
		flagregcode = false;
		// createCode();
	} else {
		$("#form-c-error2").css({
			display: "none"
		});
		flagregcode = true;
	}
});
